package cuit.game.controller;


import cuit.game.entity.Application;
import cuit.game.entity.Team;
import cuit.game.entity.User;
import cuit.game.service.ApplicationService;
import cuit.game.service.TeamService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/application")
public class ApplicationController {

    //注入类
    @Autowired
    private ApplicationService applicationService;

    @Autowired
    private TeamService teamService;

    //返回学生报名信息
    @GetMapping("/listStudent")
    public String list(HttpServletRequest request, Model model) {
        // 获取HttpSession对象
        HttpSession session = request.getSession();

        // 获取用户ID
        User user = (User) session.getAttribute("user");
        long userId = user.getId();

        //获得对应学生的报名信息
        List<Application> student = applicationService.findByUser_id(userId);
        model.addAttribute("student", student);
        return "user/contest";
    }

    //返回老师需要的报名信息
    @GetMapping("/listTeam")
    public String list(@RequestParam("teamId") long TeamId, Model model) {

        //获得对应队伍的报名信息
        List<Application> team = applicationService.findByTeam_id(TeamId);
        model.addAttribute("team", team);
        return "list";
    }

    //删除对应报名信息--返回个人
    @GetMapping("delete")
    public String  delete(@RequestParam("Id") long id,HttpServletRequest request, Model model) {

        //修改相关队伍的信息
        Application application = applicationService.findById(id);
        long teamId = application.getTeam().getId();
        Team team = teamService.findById(teamId);


        // 获取HttpSession对象
        HttpSession session = request.getSession();

        // 获取用户ID
        User user = (User) session.getAttribute("user");
        long userId = user.getId();

        //判断是否是队长
        if(team.getUser().getId() != userId){
            team.setBody(team.getBody()-1);

            teamService.save(team);

            //删除对应报名信息
            applicationService.deleteById(id);
        }
        else {
            List<Application> applications = applicationService.findByTeam_id(teamId);
            for(Application app : applications){
                applicationService.deleteById(app.getId());
            }
            teamService.deleteById(teamId);
        }

        List<Team> teams = teamService.findAll();
        model.addAttribute("teamlist", teams);
        return "team/list";
 
    }

  //删除对应报名信息--返回队伍
    @GetMapping("delete1")
    public String  delete1(@RequestParam("Id") long id,HttpServletRequest request, Model model) {

        //修改相关队伍的信息
        Application application = applicationService.findById(id);
        long teamId = application.getTeam().getId();
        Team team = teamService.findById(teamId);


        // 获取HttpSession对象
        HttpSession session = request.getSession();

        // 获取用户ID
        User user = (User) session.getAttribute("user");
        long userId = user.getId();

        //判断是否是队长
        if(team.getUser().getId() != userId){
            team.setBody(team.getBody()-1);

            teamService.save(team);

            //删除对应报名信息
            applicationService.deleteById(id);
        }
        else {
            List<Application> applications = applicationService.findByTeam_id(teamId);
            for(Application app : applications){
                applicationService.deleteById(app.getId());
            }
            teamService.deleteById(teamId);
        }

        //获得对应学生的报名信息
        List<Application> teams = applicationService.findByTeam_id(teamId);
        model.addAttribute("student", teams);
        return "user/contest";
    }
    

    
  //返回对应队伍的报名信息
    @GetMapping("/view")
    public String view(@RequestParam("teamId") long teamId, Model model) {
        
        List<Application> team = applicationService.findByTeam_id(teamId);
        model.addAttribute("student", team);
        return "user/contest";
    }
    
    

}
